package org.lk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.lk.reggie.common.BaseContext;
import org.lk.reggie.common.R;
import org.lk.reggie.entity.AddressBook;
import org.lk.reggie.entity.ShoppingCart;
import org.lk.reggie.entity.User;
import org.lk.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController
{

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List<AddressBook>> getAllAddressBook()
    {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook :: getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }


    @GetMapping("/{id}")
    public R<AddressBook> getAddressById(@PathVariable Long id)
    {
        AddressBook byId = addressBookService.getById(id);
        if (byId != null)
            return R.success(byId);
        return R.error("error");
    }


    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook)
    {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);

        boolean save = addressBookService.save(addressBook);
        if (save)
            return R.success("success");
        return R.error("error");
    }


    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook)
    {
        boolean b = addressBookService.updateById(addressBook);
        if (b)
            return R.success("success");
        return R.error("error");
    }


    @PutMapping("/default")
    public R<String> defaultAddressBook(@RequestBody AddressBook addressBook)
    {
        // 获取当前用户 id
        Long currentId = BaseContext.getCurrentId();
        // 根据当前用户 id 查询其所以地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook :: getUserId, currentId);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        // 将指定地址设为默认地址
        for (AddressBook aB:
             list)
        {
//            if (aB.getId() == addressBook.getId())
//                aB.setIsDefault(1);
//            else
              aB.setIsDefault(0);
            addressBookService.updateById(aB);
        }
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("success");
    }


    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress()
    {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook :: getUserId, currentId);
        queryWrapper.eq(AddressBook :: getIsDefault, 1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        System.out.println(one);
        return R.success(one);
    }
}
